from rest_framework import viewsets, status
from django.db.models import Q
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK

from apps.inform.models import Inform, InformRead
from apps.inform.serializers import InformSerializer, ReadInformSerializer
from rest_framework.views import APIView
from django.db.models import Prefetch

class InformViewSet(viewsets.ModelViewSet):
    queryset = Inform.objects.all()
    serializer_class = InformSerializer

    '''
        通知列表：
            inform.public = True
            inform.departments
            inform.author = request.user
    '''
    def get_queryset(self):
        # quersets = self.queryset.prefetch_related('reads').filter(
        #                                         Q(public=True) |
        #                                         Q(departments=self.request.user.department) |
        #                                         Q(author=self.request.user)
        #                                     ).distinct()
        quersets = self.queryset.select_related('author').prefetch_related(Prefetch(
            'reads', queryset=InformRead.objects.filter(user_id=self.request.user.uid)
        ), 'departments').filter(
                                                Q(public=True) |
                                                Q(departments=self.request.user.department) |
                                                Q(author=self.request.user)
                                            ).distinct()
        return quersets
        # for querset in quersets:
        #     querset.is_read = InformRead.objects.filter(inform=querset, user=self.request.user).exists()

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        if instance.author.uid == request.user.uid:
            self.perform_destroy(instance)
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            return Response(status=status.HTTP_401_UNAUTHORIZED)
    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        data = serializer.data
        data['read_count'] = InformRead.objects.filter(inform_id=instance.id).count()
        return Response(data=data, status=status.HTTP_200_OK)

class ReadInformView(APIView):
    def post(self, request, *args, **kwargs):
        serializer = ReadInformSerializer(data=request.data)
        if serializer.is_valid():
            inform_pk = serializer.validated_data.get('inform_pk')
            if InformRead.objects.filter(id=inform_pk, user_id=request.user.uid).exists():
                return Response()
            else:
                try:
                    InformRead.objects.create(inform_id=inform_pk, user_id=request.user.uid)
                except Exception as e:
                    print(e)
                    return Response(status=HTTP_200_OK)
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            return Response({'detail': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)